home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Tool Chest / Text / WASTE 1.3a6 / Demo / Source / LongControls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-12-01  |  4.2 KB  |  177 lines  |  [TEXT/CWIE]

  1. /*
  2.     WASTE Demo Project:
  3.     Macintosh Controls with Long Values
  4.  
  5.     Copyright © 1993-1997 Marco Piovanelli
  6.     All Rights Reserved
  7.  
  8.     C port by John C. Daub
  9. */
  10.  
  11. #ifndef __CONTROLS__
  12. #include <Controls.h>
  13. #endif
  14.  
  15. #ifndef __FIXMATH__
  16. #include <FixMath.h>
  17. #endif
  18.  
  19. #ifndef __TOOLUTILS__
  20. #include <ToolUtils.h>
  21. #endif
  22.  
  23. #ifndef __WEDEMOAPP__
  24. #include "WEDemoIntf.h"
  25. #endif
  26.  
  27.  
  28. // long control auxiliary record used for keeping long settings
  29. // a handle to this record is stored in the reference field of the control record
  30.  
  31. struct LCAuxRec
  32. {
  33.     SInt32    value;    // long value
  34.     SInt32    min;    // long min
  35.     SInt32    max;    // long max
  36. };
  37. typedef struct LCAuxRec LCAuxRec, *LCAuxPtr, **LCAuxHandle;
  38.  
  39. OSErr LCAttach ( ControlRef inControl )
  40. {
  41.     LCAuxHandle aux ;
  42.  
  43.     //    allocate the auxiliary record that will hold long settings
  44.     if ( ( aux = ( LCAuxHandle ) NewHandleClear ( sizeof ( LCAuxRec ) ) ) == nil )
  45.     {
  46.         return    MemError ( ) ;
  47.     }
  48.  
  49.     //    store a handle to the auxiliary record in the contrlRfCon field
  50.     SetControlReference ( inControl, ( SInt32 ) aux ) ;
  51.  
  52.     //    copy current control settings into the auxiliary record
  53.     ( * aux ) -> value = GetControlValue ( inControl ) ;
  54.     ( * aux ) -> min = GetControlMinimum ( inControl ) ;
  55.     ( * aux ) -> max = GetControlMaximum ( inControl ) ;
  56.  
  57.     return noErr ;
  58. }
  59.  
  60. void LCDetach ( ControlRef inControl )
  61. {
  62.     Handle aux ;
  63.  
  64.     if ( ( aux = ( Handle ) GetControlReference ( inControl ) ) != nil )
  65.     {
  66.         SetControlReference ( inControl, 0L ) ;
  67.         DisposeHandle ( aux ) ;
  68.     }
  69. }
  70.  
  71. void LCSetValue ( ControlRef inControl, SInt32 inValue )
  72. {
  73.     LCAuxHandle aux ;
  74.     SInt16 controlMin, controlMax, newControlValue ;
  75.  
  76.     aux = ( LCAuxHandle ) GetControlReference ( inControl ) ;
  77.  
  78.     //    make sure inValue is in the range min...max
  79.     if ( inValue < ( * aux ) -> min )
  80.     {
  81.         inValue = ( * aux ) -> min ;
  82.     }
  83.     if ( inValue > ( * aux ) -> max )
  84.     {
  85.         inValue = ( * aux ) -> max ;
  86.     }
  87.  
  88.     //    save inValue in auxiliary record
  89.     ( * aux ) -> value = inValue ;
  90.  
  91.     //    calculate new thumb position
  92.     controlMin = GetControlMinimum ( inControl ) ;
  93.     controlMax = GetControlMaximum ( inControl ) ;
  94.     newControlValue = controlMin + FixRound ( FixMul ( FixDiv ( inValue - ( * aux ) -> min,
  95.         ( * aux ) -> max - ( * aux ) -> min), BSL ( controlMax - controlMin, 16 ) ) ) ;
  96.  
  97.     //    do nothing if the thumb position hasn't changed
  98.     if ( newControlValue != GetControlValue ( inControl ) )
  99.     {
  100.         SetControlValue ( inControl, newControlValue ) ;
  101.     }
  102. }
  103.  
  104. void LCSetMin ( ControlRef inControl, SInt32 inMin )
  105. {
  106.     LCAuxHandle aux ;
  107.  
  108.     aux = ( LCAuxHandle ) GetControlReference ( inControl ) ;
  109.  
  110.     //    make sure inMin is less than or equal to max
  111.     if ( inMin > ( * aux ) -> max )
  112.     {
  113.         inMin = ( * aux ) -> max ;
  114.     }
  115.  
  116.     //    save inMin in auxiliary record
  117.     ( * aux ) -> min = inMin ;
  118.  
  119.     //    set control minimum to inMin or SHRT_MIN, whichever is greater
  120.     SetControlMinimum ( inControl, ( inMin >= SHRT_MIN ) ? inMin : SHRT_MIN ) ;
  121.  
  122.     //    reset value
  123.     LCSetValue ( inControl, ( * aux ) -> value ) ;
  124. }
  125.  
  126. void LCSetMax ( ControlRef inControl, SInt32 inMax )
  127. {
  128.     LCAuxHandle aux ;
  129.  
  130.     aux = ( LCAuxHandle ) GetControlReference ( inControl ) ;
  131.  
  132.     //    make sure inMax is greater than or equal to min
  133.     if ( inMax < ( * aux ) -> min )
  134.     {
  135.         inMax = ( * aux ) -> min ;
  136.     }
  137.  
  138.     //    save inMax in auxiliary record
  139.     ( * aux ) -> max = inMax ;
  140.  
  141.     //    set control maximum to inMax or SHRT_MAX, whichever is less
  142.     SetControlMaximum ( inControl, ( inMax <= SHRT_MAX ) ? inMax : SHRT_MAX ) ;
  143.  
  144.     //    reset value
  145.     LCSetValue ( inControl, ( * aux ) -> value ) ;
  146. }
  147.  
  148. SInt32 LCGetValue ( ControlRef inControl )
  149. {
  150.     return ( * ( LCAuxHandle ) GetControlReference ( inControl ) ) -> value ;
  151. }
  152.  
  153. SInt32 LCGetMin ( ControlRef inControl )
  154. {
  155.     return ( * ( LCAuxHandle ) GetControlReference ( inControl ) ) -> min ;
  156. }
  157.  
  158. SInt32 LCGetMax ( ControlRef inControl )
  159. {
  160.     return ( * ( LCAuxHandle ) GetControlReference ( inControl ) ) -> max ;
  161. }
  162.  
  163. void LCSynch ( ControlRef inControl )
  164. {
  165.     LCAuxHandle aux ;
  166.     SInt16 controlMin, controlMax, controlValue ;
  167.  
  168.     controlMin = GetControlMinimum ( inControl ) ;
  169.     controlMax = GetControlMaximum ( inControl ) ;
  170.     controlValue = GetControlValue ( inControl ) ;
  171.     aux = ( LCAuxHandle ) GetControlReference ( inControl ) ;
  172.  
  173.     //    calculate new long value
  174.     ( * aux ) -> value = ( * aux ) -> min + FixMul ( FixRatio ( controlValue - controlMin,
  175.                   controlMax - controlMin ), ( * aux ) -> max - ( * aux ) -> min ) ;
  176. }
  177.